package com.jack.leetcode.每日一题;

public class 最长公共前缀 {

    public static void main(String[] args) {
        String[] strs = {"dog","racecar","car"};
        System.out.println(longestCommonPrefix(strs));
    }

    public static String longestCommonPrefix(String[] strs) {
        if(strs == null){
            return "";
        }
        int n = strs.length;
        if(n == 0){
            return "";
        }
        String s = strs[0];
        int length = s.length();
        for(int i=0;i<length;i++){
            String tmp = s.substring(0,i+1);
            boolean isPrefix = true;
            for(int j = 0;j < n;j++){
                if(!strs[j].startsWith(tmp)){
                    isPrefix = false;
                }
            }
            if(isPrefix == false){
                return s.substring(0,i);
            }
        }
        return s;
    }
}
